package com.alibaba.cirtus.ots.application.adapter.ability.utils;


import java.util.List;
import java.util.Map;

/**
 * @author zhoubc
 * @description: 结果封装
 * @date 2021/9/6 11:43
 */
public class ResultUtils {

    public static String getLastInsertId(Map result){
        Map map = (Map) result.get("result");
        return (String) map.get("lastInsertId");
    }

    public static Integer getUpdateCount(Map result){
        Map map = (Map) result.get("result");
        return Integer.parseInt(map.get("count").toString());
    }
    
    

    public static DubboResult getDubboResult(Map result){
        DubboResult dubboResult = new DubboResult();
        Map map = (Map) result.get("result");
        Boolean success = (Boolean) map.get("success");
        String message = (String) map.get("message");
        Object queryResult = map.get("result");
        if(!success){
            throw new RuntimeException(message);
        }
        dubboResult.setSuccess(success);
        dubboResult.setMessage(message);
        if(queryResult != null){
            dubboResult.setResult(queryResult);
        }
        return dubboResult;
    }

    public static DubboResult getDubboResultList(Map result){
        DubboResult dubboResult = new DubboResult();
        List queryResult = (List) result.get("result");
        Boolean success = (Boolean) result.get("success");
        String message = (String) result.get("message");
        if(!success){
            throw new RuntimeException(message);
        }
        dubboResult.setSuccess(success);
        dubboResult.setMessage(message);
        if(queryResult != null){
            dubboResult.setResult(queryResult);
        }
        return dubboResult;
    }
    
    public static DubboResult getDubboPageResult(Map result){
        DubboResult dubboResult = new DubboResult();
        Map map = (Map) result.get("result");
        Boolean success = (Boolean) map.get("success");
        String message = (String) map.get("message");
        if(!success){
            throw new RuntimeException(message);
        }
        dubboResult.setSuccess(success);
        dubboResult.setMessage(message);
        Map pageResult = (Map) map.get("result");
        if (!pageResult.isEmpty()) {
            Double total = (Double) pageResult.get("total");
            if (null != total) {
                dubboResult.setTotal(Integer.valueOf(total.intValue()));
            }
            dubboResult.setResult(pageResult.get("content"));
        }

        return dubboResult;
    }

    public static DubboResult getDubboPageResultList(Map result){
        DubboResult dubboResult = new DubboResult();
        Map map = (Map) result.get("result");
        Boolean success = (Boolean) map.get("success");
        String message = (String) map.get("message");
        if(!success){
            throw new RuntimeException(message);
        }
        dubboResult.setSuccess(success);
        dubboResult.setMessage(message);
        List pageResult = (List) map.get("result");
        dubboResult.setResult(pageResult);
        return dubboResult;
    }


}
